package com.service.impl;

import com.dao.UserMapper;
import com.exception.MallException;
import com.exception.MallExceptionEnum;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.User;
import com.service.UserService;
import com.utils.MD5Utils;
import com.vo.userVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * @Author 杨冰清
 * @Date 15:38
 * @Description
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;


    @Override
    public User getUser() {
        return userMapper.selectByPrimaryKey(1);
    }

    /**
     * 注册实现类
     *
     * @param userVo
     * @throws MallException
     */
    @Override
    public void register(userVo userVo) throws MallException {
        User result = userMapper.selectByName(userVo.getUsername());
        //用户注册不允许重名
        if (result != null) {
            throw new MallException(MallExceptionEnum.NAME_EXISTED);
        }
        //将用户写到数据库
        User user = new User();
        user.setUsername(userVo.getUsername());

        //对密码进行保护
        /*try {
            user.setPassword(MD5Utils.getMD5Str(userVo.getPassword()));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }*/

        //将密码写到书库（不加密）
        user.setPassword(userVo.getPassword());
        //将电话写到数据库
        user.setTellPhone(userVo.getTellphone());
        int count = userMapper.insertSelective(user);
        if (count == 0) {
            throw new MallException(MallExceptionEnum.INSERT_FAILED);
        }
    }

    /**
     * 登录实现类
     *
     * @param username
     * @param password
     * @return
     * @throws MallException
     */
    @Override
    public User login(String username, String password) throws MallException {
        String md5Password = null;
        try {
            md5Password = MD5Utils.getMD5Str(password);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        User user = userMapper.selectLogin(username, md5Password);
        if (user == null) {
            throw new MallException(MallExceptionEnum.WRONG_PASSWORD);
        }
        return user;
    }

    /**
     * 检查是否是管理员
     *
     * @param user
     * @return
     */
    @Override
    public boolean checkAdminRole(User user) {
        //1是普通用户，2是管理员
        return user.getRole().equals(2);
    }

    @Override
    public void updateInformation(User user) throws MallException {
        //更新个性签名
        int updateCount = userMapper.updateByPrimaryKeySelective(user);
        System.out.println(updateCount);
        if (updateCount != 1) {
            throw new MallException(MallExceptionEnum.UPDATE_FAILED);
        }
    }

    @Override
    public void updateUserTell(User user) throws MallException {
        //更新手机号
        int updateCount = userMapper.updateByPrimaryKeySelective(user);
        if (updateCount != 1){
            throw new MallException(MallExceptionEnum.UPDATE_FAILED);
        }
    }

    @Override
    public void updatePass(User user) throws MallException {
        int updateCount = userMapper.updateByPrimaryKeySelective(user);
        if (updateCount != 1){
            throw new MallException(MallExceptionEnum.UPDATE_FAILED);
        }
    }


    /**
     * 分页查询用户信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo AdminListForUser(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> userList = userMapper.AdminListForUser();
        PageInfo pageInfo = new PageInfo(userList);
        return pageInfo;
    }



    /**
     * 不分页查询用户信息
     * @return
     */
    @Override
    public List<User> list() {
        return userMapper.AdminListForUser();
    }

    @Override
    public User loginIn(userVo userVo) {
        /*String md5Password = null;
        try {
            md5Password = MD5Utils.getMD5Str(userVo.getPassword());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }*/
        User user = userMapper.selectLogin(userVo.getUsername(), userVo.getPassword());
        if (user == null) {
            throw new MallException(MallExceptionEnum.WRONG_PASSWORD);
        }
        return user;
    }

    @Override
    public User listUserMsg(Integer id) {
        return null;
    }


}

